//
//  TableViewController.m
//  CarHome
//
//  Created by 苗钧豪 on 16/8/9.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "TableViewController.h"
#import "CarHome.h"
#import "TableViewCell.h"
#import "myVIew.h"
#import "BaseModel.h"
#import "FindCar.h"
#import "Result.h"
#import "Brand.h"
#import "Brandlist.h"
@interface TableViewController ()
@property(nonatomic,strong)NSMutableArray *bigArr;
@property(nonatomic,strong)NSMutableArray *smallArr;
@property(nonatomic,strong) FindCar *findcar;
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getup];
    myVIew *myview = [[myVIew alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 400)];
    self.tableView.tableHeaderView = myview;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)getup {
    self.bigArr = [NSMutableArray array];
    self.smallArr = [NSMutableArray array];
    NSString *CarJSON = [[NSBundle mainBundle] pathForResource:@"Car" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:CarJSON];
    NSError *error = nil;
    NSDictionary *Dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:&error];
    self.findcar = [FindCar modelWithDic:Dic];
    
//    NSArray *arr = Dic[@"result"][@"brandlist"];
//    for (NSDictionary *dic in arr) {
//        CarHome *car = [CarHome carHomeWithDic:dic];
//        [_bigArr addObject:car];
//        NSArray *array = [NSArray array];
//        array = dic[@"list"];
//        for (NSDictionary *dictionary in array) {
//            CarHome *car = [CarHome carHomeWithDic:dictionary];
//            [_smallArr addObject:car];
//            [_bigArr addObject:_smallArr];
//        }
//    }
}
// 索引
//-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
//{
//    NSMutableArray *arr  = [NSMutableArray array];
//    for (CarHome *car in _bigArr) {
//        [arr addObject:car.letter];
//    }
//    NSString *str = @"热";
//    [arr insertObject:str atIndex:0];
//    return arr;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
// 分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.findcar.result.brandlist.count;
}
// 每个分区的row
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    NSLog(@"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%ld",self.findcar.result.brandlist[section].list.count);
    return self.findcar.result.brandlist[section].list.count;
//    return 21;
}
// 标题title
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    NSLog(@"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%@",_findcar.result.brandlist[section].letter);
    NSString *str = _findcar.result.brandlist[section].letter;
//    NSString *str = @"a";
    return str;
}
// 每个cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    Brand *brand = self.findcar.result.brandlist[indexPath.section].list[indexPath.row];
    // Configure the cell...
//    cell.car = _smallArr[0];
    NSURL *url = [NSURL URLWithString:brand.imgurl];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *img = [UIImage imageWithData:data];
    cell.imgview.image = img;
    cell.namelabel.text = brand.name;
//    [_smallArr removeObjectAtIndex:0];
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
